//
//  YKPageWithSearchViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKPageWithSearchViewController.h"

@interface YKPageWithSearchViewController ()<UISearchBarDelegate>
@property (nonatomic, strong)NSString *oldTitle;
@end

@implementation YKPageWithSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onSearchItemTapped:)];
    self.navigationItem.rightBarButtonItem = searchItem;
}

- (void)onSearchItemTapped:(id)sender {
    self.oldTitle = self.title;
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    searchBar.showsCancelButton = NO;
    searchBar.delegate = self;
    self.navigationItem.titleView = searchBar;
    self.navigationItem.titleView.tintColor = [UIColor blueColor];
    UIBarButtonItem *cancleItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(onCancleItemTapped:)];
    self.navigationItem.rightBarButtonItem = cancleItem;
    [searchBar becomeFirstResponder];
}
- (void)onCancleItemTapped:(id)sender {
    UISearchBar *searchBar = (UISearchBar *)self.navigationItem.titleView;
    searchBar.text = @"";
    [self beginSearchWithText:@""];
    self.navigationItem.titleView = nil;
    self.title = self.oldTitle;
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onSearchItemTapped:)];
    self.navigationItem.rightBarButtonItem = searchItem;
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar endEditing:YES];
    [self beginSearchWithText:searchBar.text];
}

- (void)beginSearchWithText:(NSString *)text {
    
}
- (NSString *)searchText {
    UISearchBar *searchBar = (UISearchBar *)self.navigationItem.titleView;
    return searchBar.text?:@"";
}
@end
